﻿#region Usings
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

using ghrb.Camera;
#endregion

namespace ghrb.Gameplay
{
    //Handles the highway stuff
    class Highway
    {
        #region Public Functions
        /// <summary>
        /// Load all highway content
        /// </summary>
        /// <param name="content">Content manager</param>
        public void LoadContent(ContentManager content, float aspectRat)
        {
            //Load highway model
            m_mdlHighway = content.Load<Model>("Models/Gameplay/mdlHighway");

            //Load Diffuse texture
            m_texDiffuse = content.Load<Texture2D>("Textures/Highway/hDiffuse");

            //Load Alpha texture
            m_texAlpha = content.Load<Texture2D>("Textures/Highway/hAlpha");

            //Load highway effect file
            m_eHighway = content.Load<Effect>("Effect Files/Highway");

            //Create camera
            m_camHighway = new CameraHighway(aspectRat);

            //Set blend state
            m_eHighway.GraphicsDevice.BlendState = BlendState.AlphaBlend;
            m_eHighway.GraphicsDevice.DepthStencilState = DepthStencilState.DepthRead;

            //Set parameters of the highway
            m_eHighway.Parameters["gDiffuse"].SetValue(m_texDiffuse);
            m_eHighway.Parameters["gAlpha"].SetValue(m_texAlpha);
            m_eHighway.Parameters["gProjection"].SetValue(m_camHighway.Projection);
            m_eHighway.Parameters["gView"].SetValue(m_camHighway.View);

        }

        public void Draw(float offset)
        {
            //Get transformation bones
            Matrix[] transforms = new Matrix[m_mdlHighway.Bones.Count];
            m_mdlHighway.CopyAbsoluteBoneTransformsTo(transforms);

            //Go over all meshes in the model
            foreach (ModelMesh meshes in m_mdlHighway.Meshes)
            {
                //Go over every part
                foreach (ModelMeshPart parts in meshes.MeshParts)
                {
                    //Set effect values
                    m_eHighway.Parameters["gWorld"].SetValue(transforms[meshes.ParentBone.Index] * Matrix.Identity);
                    m_eHighway.Parameters["gOffset"].SetValue(offset);

                    parts.Effect = m_eHighway;
                }
                meshes.Draw();
            }
        }
        #endregion

        #region Properties and Vars
        /// <summary>
        /// Model of the highway
        /// </summary>
        private Model m_mdlHighway = null;

        /// <summary>
        /// Diffuse texture of the highway
        /// </summary>
        private Texture2D m_texDiffuse = null;

        /// <summary>
        /// Alpha texture of the highway
        /// </summary>
        private Texture2D m_texAlpha = null;

        /// <summary>
        /// Effect to render the highway
        /// </summary>
        private Effect m_eHighway = null;

        private CameraHighway m_camHighway = null;
        #endregion
    }
}
